﻿#include "Measure_Side.h"
#include "qdebug.h"
Measure_Side::Measure_Side()
{
    /*CreateMetrologyModel(&MetrologyHandle1);
    CreateMetrologyModel(&MetrologyHandle2);
    CreateMetrologyModel(&MetrologyHandle3);*/
    CameraParameters.Clear();
    CameraParameters[0] = 0;
    CameraParameters[1] = -0.940741;
    CameraParameters[2] = 1.14751e-005;
    CameraParameters[3] = 1.14709e-005;
    CameraParameters[4] = 891.283;
    CameraParameters[5] = 943.919;
    CameraParameters[6] = 2000;
    CameraParameters[7] = 1500;
//    GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");
}
int Measure_Side::M_Side(HObject Img, HTuple &OutPara, HObject &OutImg)
{
    Flag11 = true;
    HObject  Region, ConnectedRegions;
    HObject  ImageMapped, SelectedRegions, RegionTrans;
    HTuple  Area, Row, Column,W,H;
    HTuple  CameraParameters;
    HTuple  w, h,Index, line1, line2, line3;
    HTuple  Line1, Line2, Line3;
    HTuple  Distance1, Distance2, Distance3, Distance4, D;
    HTuple  MetrologyHandle1,MetrologyHandle2,MetrologyHandle3;

    /*
    //换算比值
    float para1=0.011468;
    //补偿值
    float para2=-0.01549335;
    //——————————————————————垂直方向
    //补偿值
    float para3 = 0;
    //标准值w
    float para4 = 2.18;
    //上限值
    float para5 = 0.01;
    //下限值
    float para6 = 0.01;
    //------------------图像处理

    //粗定定位阈值
    float para7=10;
    //找边sigma
    float para8=1;
    //找边阈值
    float para9=50;
*/


    //换算比例
    float para1 = m_part.para1;
    //总体补偿
    float para2 = m_part.para2;
    //——————————————————————垂直方向
    //补偿值
    float para3 = m_part.para3;
    //标准值
    float para4 = m_part.para4;
    //上限值
    float para5 = m_part.para5;
    //下限值
    float para6 = m_part.para6;
    //粗定定位阈值
    float para7 = m_part.para7;
    //找边sigma
    float para8 = m_part.para8;
    //找边阈值
    float para9 = m_part.para9;
    //标志位
    int flag=0;

    Flag1 = true;
    try
    {
        OutImg = Img;
        GetImageSize(Img, &W, &H);
        if ((W != 2000) || (H != 1500))
        {
            OutPara = 0;
            Flag1 = false;
            flag = 2;
            return flag;
        }
        
        CameraParameters.Clear();
        CameraParameters[0] = 0;
        CameraParameters[1] = -0.940741;
        CameraParameters[2] = 1.14751e-005;
        CameraParameters[3] = 1.14709e-005;
        CameraParameters[4] = 891.283;
        CameraParameters[5] = 943.919;
        CameraParameters[6] = 2000;
        CameraParameters[7] = 1500;
        GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");
		MapImage(Img, Map, &ImageMapped);
        Threshold(ImageMapped, &Region, 0, para7);
        Connection(Region, &ConnectedRegions);
        SelectShape(ConnectedRegions, &ConnectedRegions, "column", "and", 300,
                    1640);
        SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);
        ShapeTrans(SelectedRegions, &RegionTrans, "rectangle2");
        AreaCenter(RegionTrans, &Area, &Row, &Column);
        RegionFeatures(RegionTrans, "width", &w);
        RegionFeatures(RegionTrans, "height", &h);


        //物体上表面
        line1.Clear();
        line1.Append(Row-(h/2));
        line1.Append(Column-(w/3));
        line1.Append(Row-(h/2));
        line1.Append(Column+(w/3));

        //玻璃边
        line2.Clear();
        line2.Append(Row+(h/2));
        line2.Append((Column+(w/2))+10);
        line2.Append(Row+(h/2));
        line2.Append(((Column+(w/2))+10)+300);
        line3.Clear();
        line3.Append(Row+(h/2));
        line3.Append((Column-(w/2))-10);
        line3.Append(Row+(h/2));
        line3.Append(((Column-(w/2))-10)-300);

        if ((line1[0].D() < 0) || (line2[0].D() < 0) || (line3[0].D() < 0))
        {
            OutPara = 0;
            Flag1 = false;
            return 2;
        }
        CreateMetrologyModel(&MetrologyHandle1);
        AddMetrologyObjectGeneric(MetrologyHandle1, "line", line1, 20, 5, para8, para9, HTuple(), HTuple(), &Index);
        ApplyMetrologyModel(ImageMapped, MetrologyHandle1);
        GetMetrologyObjectResult(MetrologyHandle1, "all", "all", "result_type", "all_param", &Line1);
        ClearMetrologyModel(MetrologyHandle1);


        CreateMetrologyModel(&MetrologyHandle2);
        AddMetrologyObjectGeneric(MetrologyHandle2, "line", line2, 20, 5, para8, para9,
                                  HTuple(), HTuple(), &Index);
        ApplyMetrologyModel(ImageMapped, MetrologyHandle2);
        GetMetrologyObjectResult(MetrologyHandle2, "all", "all", "result_type", "all_param", &Line2);
        ClearMetrologyModel(MetrologyHandle2);


        CreateMetrologyModel(&MetrologyHandle3);
        AddMetrologyObjectGeneric(MetrologyHandle3, "line", line3, 20, 5, para8, para9, HTuple(), HTuple(), &Index);
        ApplyMetrologyModel(ImageMapped, MetrologyHandle3);
        GetMetrologyObjectResult(MetrologyHandle3, "all", "all", "result_type", "all_param", &Line3);
        ClearMetrologyModel(MetrologyHandle3);

        DistancePl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line2[0]), HTuple(Line2[1]),
                HTuple(Line2[2]), HTuple(Line2[3]), &Distance1);
        DistancePl(HTuple(Line1[2]), HTuple(Line1[3]), HTuple(Line2[0]), HTuple(Line2[1]),
                HTuple(Line2[2]), HTuple(Line2[3]), &Distance2);


        DistancePl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line3[0]), HTuple(Line3[1]),
                HTuple(Line3[2]), HTuple(Line3[3]), &Distance3);
        DistancePl(HTuple(Line1[2]), HTuple(Line1[3]), HTuple(Line3[0]), HTuple(Line3[1]),
                HTuple(Line3[2]), HTuple(Line3[3]), &Distance4);
		
        D = (Distance1+Distance2+Distance3+Distance4)/4;
		double aaa = D.D();
        OutPara = (D*para1)+para2+para3;
        if((OutPara>(para4-0.08))&&(OutPara<(para4+0.08)))
        {
            if(!(((OutPara)>(para4-para6))&&((OutPara)<(para4+para5))))
            {
                if ((OutPara)<(para4 - para6))
                {
                    Flag11 = false;
                }
                Flag1=false;
                flag=1;
            }

        }
        else
        {
            flag=-1;
            Flag1=false;
        }

        return flag;

    }
    catch(HException ex)
    {
        OutPara=0;
        Flag1=false;
        flag=2;
        return flag;
    }

}
